import { Context } from "egg";

export const routeSymbolKey = Symbol.for("dinegg#Route");

/** 函数名绑定的路由列表，key为函数名 */
export type RouteMetaData = Record<
	string,
	{ method: string; path: string; middlewares?: MiddlewaresType }[]
>;

export type MiddlewaresType = ((ctx: Context, next: () => Promise<void>) => Promise<void>)[];

export function setRouteMetaData(obj: Object, value: RouteMetaData) {
	return Reflect.defineMetadata(routeSymbolKey, value, obj,"routes");
}

export function getRouteMetaData(obj: Object) {
	let value = Reflect.getMetadata(routeSymbolKey, obj,"routes") as RouteMetaData;
	if (!value) {
		value = {};
		setRouteMetaData(obj, value);
	}
	return value;
}
